package test;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class Http {
    public  static void main(String[] args)
    {
        //System.out.println("args = [" + args + "]");
        try {
            Map <String,String>map=new TreeMap<>();
            //Map<String,String> map=new LinkedHashMap<>();
            //Map<String,String> map=new TreeMap<>();
            //Map<String,String> map=new LinkedHashMap<>();
            map.put("f_id","100");
            map.put("f_gcmc","");
            map.put("f_yqxh","");
            //map.put()
            String result= getDataFromHttp("http://localhost:8080/zzq/handbook/retrievehandbooksby",map);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            Map <String,String>map=new HashMap<String,String>();
            map.put("f_id","100");
            map.put("f_gcmc","");
            map.put("f_yqxh","");
            String result= getDataFromHttpOG("http://localhost:8080/zzq/handbook/retrievehandbooksby",map);
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * Get请求
     * @param url    	访问的地址
     * @param params 	参数列表
     * @return		返回的html或者json
     */
    public static String getDataFromHttp(String url, Map<String, String> params) throws Exception {
        if(url == null || url.trim().isEmpty())
            return null;
        //创建一个HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //get请求根据参数拼接好url
        if(params != null && params.size() > 0) {
            url += "?";
            for(String key: params.keySet()) {
                url += (key + "=" + params.get(key) + "&");
            }
        }
        //创建一个HttpGet对象，这里需要指定一个请求的url
        HttpGet get = new HttpGet(url);
        //执行http请求
        CloseableHttpResponse response = httpClient.execute(get);
        //接收返回结果的对象
        HttpEntity entity = response.getEntity();
        //获取响应内容数据
        String result = EntityUtils.toString(entity);
        //关闭流
        response.close();
        httpClient.close();
        //返回
        return result;
    }

    /**
     * 发起一次Get请求并返回接收到的数据
     */
    //这里可以自行将参数拼接到url中，也可以改我的代码。。。随你便吧
    public static String getDataFromHttpOG(String url,Map<String, String> params) throws IOException {
        if(url == null || url.trim().isEmpty())
            return null;
        if(params != null && params.size() > 0) {
            url += "?";
            for(String key: params.keySet()) {
                url += (key + "=" + params.get(key) + "&");
            }
        }
        //定义一个StringBuffer，用于储存结果
        StringBuffer result = new StringBuffer();
        //定义一个url对象
        URL url2 = new URL(url);
        //创建一个连接请求对象
        HttpURLConnection conn = (HttpURLConnection)url2.openConnection();
        //规定请求的方式
        conn.setRequestMethod("GET");
        //执行
        conn.connect();
        //使用一个输入流接收数据
        InputStream is = conn.getInputStream();
        //!@#$^&*
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String readStr = null;
        //将数据一行一行的存入StringBuffer
        while((readStr = br.readLine()) != null) {
            result.append(readStr);
            result.append("\r\n");
        }
        //关闭流
        br.close();
        //返回数据
        return result.toString();
    }
}
